﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class ePioneer_ep_pioneer_knowledges_edit : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            CMSModel.CMSEntities dal = DAL.DBManager.GetCmsEmtity();
            ddlepType.DataSource = dal.ePioneer_Type;
            ddlepType.DataTextField = "name";
            ddlepType.DataValueField = "id";
            ddlepType.DataBind();
            if (Request["id"] != null)
            {

                int id = int.Parse(Request["id"].ToString());
                if (id > 0)
                {
                    try
                    {
                        CMSModel.ePioneer_Knowledges epK = dal.ePioneer_Knowledges.First(x => x.id == id);

                        ddlepType.SelectedValue = epK.typeid.ToString();
                        txt_content.Text = epK.knowledges;
                    }
                    catch { }
                }
            }
        }
    }
    /// <summary>
    /// 修改
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void BtnModify_Click(object sender, EventArgs e)
    {

        if (Request["id"] != null)
        {
            int id = int.Parse(Request["id"].ToString());
            //如果不为0，则为修改

            try
            {
                CMSModel.CMSEntities dal = DAL.DBManager.GetCmsEmtity();
                CMSModel.ePioneer_Knowledges epK = dal.ePioneer_Knowledges.First(x => x.id == id);
                epK.knowledges = txt_content.Text;
                epK.typeid = int.Parse(ddlepType.SelectedValue);
                dal.SaveChanges();
                ClientScript.RegisterStartupScript(GetType(), "aa", "alert('修改成功');document.location='ep_pioneer_knowledges.aspx'", true);
            }
            catch (Exception)
            {
                ClientScript.RegisterStartupScript(GetType(), "aa", "alert('修改失败');", true);
            }
        }
        //如果为0，则视为添加
        else
        {
            try
            {
                CMSModel.CMSEntities dal = DAL.DBManager.GetCmsEmtity();
                CMSModel.ePioneer_Knowledges epK = new CMSModel.ePioneer_Knowledges();
                epK.typeid = int.Parse(ddlepType.SelectedValue);
                epK.knowledges = txt_content.Text;
                dal.AddToePioneer_Knowledges(epK);
                dal.SaveChanges();
                ClientScript.RegisterStartupScript(GetType(), "aa", "alert('添加成功');document.location='ep_pioneer_knowledges.aspx'", true);
            }
            catch (Exception)
            {
                ClientScript.RegisterStartupScript(GetType(), "aa", "alert('添加失败');", true);
            }
        }
    }
}